package com.katze.boot.common.converter;

import cn.idev.excel.converters.date.DateStringConverter;
import cn.idev.excel.metadata.GlobalConfiguration;
import cn.idev.excel.metadata.data.WriteCellData;
import cn.idev.excel.metadata.property.ExcelContentProperty;
import com.katze.common.DatePattern;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;


public class DateTimeStringConverter extends DateStringConverter {
    private static final Logger log = LoggerFactory.getLogger(DateTimeStringConverter.class);

    @Override
    public WriteCellData<?> convertToExcelData(Date value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
        try {
            if (value == null)
                return new WriteCellData<>("");
            else {
                SimpleDateFormat sdf = new SimpleDateFormat(DatePattern.JDK_DATETIME_PATTERN, Locale.US);
                return new WriteCellData<>(DatePattern.NORM_DATETIME_FORMAT.format(sdf.parse(value.toString())));
            }
        } catch (Exception e) {
            log.error("时间转换异常", e);
            return new WriteCellData<>("");
        }
    }
}
